import 'package:bruno/bruno.dart';
import 'package:whale/helper/global/user/logic.dart';

import '../../helper/utils/util.dart';
import 'index.dart';

class AppTabController extends GetxController {
  AppTabController();

  final state = AppTabState();

  void clickTab(int index) {
    if (state.currentIndex.value == index) {
      return;
    }
    if (!UserController().isLogin) {
      if (index == 3 || index == 1 || index == 2) {
        Get.toNamed(RouterPath.login);
        return;
      }
    }

    state.currentIndex.value = index;
    state.pageController.jumpToPage(index);
  }

  void setupData() {
    state.items = [
      TabModel(
        icon: 'tab_home_n.png',
        activeIcon: 'tab_home_y.png',
        title: '首页',
      ),
      TabModel(
        icon: 'tab_means_n.png',
        activeIcon: 'tab_means_y.png',
        title: '资料',
      ),
      TabModel(
        icon: 'tab_watch_n.png',
        activeIcon: 'tab_watch_y.png',
        title: '监测',
      ),
      TabModel(
        icon: 'tab_report_n.png',
        activeIcon: 'tab_report_y.png',
        title: '报告',
      ),
      TabModel(
        icon: 'tab_me_n.png',
        activeIcon: 'tab_me_y.png',
        title: '我的',
      ),
    ];
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
    setupData();
    UserController().isLoginObs.listen((p0) {
      if (!p0) {
        var index = state.currentIndex.value;
        if (index == 3 || index == 1 || index == 2) {
          Get.toNamed(RouterPath.login);
          return;
        }
      }
    });

    state.intro = BrnGuide(
      stepCount: 3,
      introMode: GuideMode.force,

      /// use defaultTheme, or you can implement widgetBuilder function yourself
      widgetBuilder: StepWidgetBuilder.useDefaultTheme(
        showClose: true,
        tipInfo: [
          BrnTipInfoBean("扫码登录", "这里是扫码登录，可以扫描web端二维码快速登录", ""),
          BrnTipInfoBean("搜索栏", "这里是搜索栏，你可以在这里进行搜索", ""),
          BrnTipInfoBean("内容页", "这里是内容详情", ''),
        ],
      ),
    );
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }
}
